//
//  InfomationEditViewController.m
//  家校互通
//
//  Created by 李赛 on 15/9/8.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "InfomationEditViewController.h"
#import "MyInfoTitleEditViewCell.h"
#import "MyInfoHeaderViewCell.h"
#import "APService.h"
@interface InfomationEditViewController ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    
}
@property (nonatomic, retain)NSMutableArray  *titleArray;
@property (nonatomic, retain)NSMutableArray  *infoDetailArray;
@property (nonatomic, weak) UITextField *cellTextField;
@end

@implementation InfomationEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"个人资料";
    
    _contentTableView.delegate=self;
    _contentTableView.dataSource=self;
    if ([DSFUserInfo shareUserInstance].isTeachLogin) {
        _titleArray = [NSMutableArray arrayWithObjects:@"姓名",@"身份",@"班级", nil];
        _infoDetailArray=[NSMutableArray arrayWithObjects:@"请修改您的姓名",@"请修改你的职位",@"请修改您的班级", nil];
    }else
    {
        _titleArray = [NSMutableArray arrayWithObjects:@"孩子姓名",@"与孩子关系",@"班级",@"手机",@"住址", nil];
        _infoDetailArray=[NSMutableArray arrayWithObjects:@"请修改您孩子的姓名",@"与孩子关系",@"班级",@"手机号" ,@"住址详细到单元号",nil];
    }
    
    UIButton *button=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 44)];
    [button setTitle:@"完成修改" forState:UIControlStateNormal];
    [button.titleLabel setFont:[UIFont systemFontOfSize:16]];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [button addTarget:self action:@selector(doneWithEditAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:button];
//    UITapGestureRecognizer *tapGesture=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideKeyBoardView)];
//    [self.view addGestureRecognizer:tapGesture];
    // Do any additional setup after loading the view.
}

-(void)doneWithEditAction:(UIButton *)sender
{
    
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)viewWillAppear:(BOOL)animated
{
      [self.navigationController setNavigationBarHidden:NO animated:YES];
    [IQKeyboardManager sharedManager].enable = YES;
    [[IQKeyboardManager sharedManager] setShouldPlayInputClicks:YES];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:YES];
    [[IQKeyboardManager sharedManager] setShouldShowTextFieldPlaceholder:YES];
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:YES];
//    [[IQKeyboardManager sharedManager] setToolbarManageBehaviour:YES];
    [IQKeyboardManager sharedManager].enableAutoToolbar = YES;
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[IQKeyboardManager sharedManager] setEnable:NO];
    [[IQKeyboardManager sharedManager] setShouldPlayInputClicks:NO];
    [[IQKeyboardManager sharedManager] setShouldResignOnTouchOutside:NO];
    [[IQKeyboardManager sharedManager] setShouldShowTextFieldPlaceholder:NO];
    [[IQKeyboardManager sharedManager] setShouldToolbarUsesTextFieldTintColor:NO];
    [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
}
//-(void)hideKeyBoardView
//{
//    [_cellTextField resignFirstResponder];
//}
#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ( [DSFUserInfo shareUserInstance].isTeachLogin) {
        if (section==0) {
            return 1;
        }else
        {
            return 3;
        }
        
    }else
    {
        if (section==0) {
            return 1;
        }else if(section==1)
        {
            return 3;
        }else
        {
            return 2;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MyInfoHeaderViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"MyInfoHeaderViewCellEdit" forIndexPath:indexPath];
        
        [vCell.userHeaderImg sd_setBackgroundImageWithURL:[NSURL URLWithString:[DSFUserInfo shareUserInstance].icon] forState:UIControlStateNormal  placeholderImage:[UIImage imageNamed:@"icon01.jpg"]];
        
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        return vCell;
    }
    else
    {
        MyInfoTitleEditViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"MyInfoTitleEditViewCellEdit" forIndexPath:indexPath];
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        vCell.infoLB.hidden=YES;
        vCell.infoEditTextField.hidden=NO;
        
        if ( [DSFUserInfo shareUserInstance].isTeachLogin) {
            vCell.infoDescriptionLB.text=[_titleArray objectAtIndex: indexPath.row];
            vCell.infoEditTextField.placeholder=[_infoDetailArray objectAtIndex:indexPath.row];
        }else
        {
            if(indexPath.section==1)
            {
                vCell.infoDescriptionLB.text=[_titleArray objectAtIndex:indexPath.row];
                vCell.infoEditTextField.placeholder=[_infoDetailArray objectAtIndex:indexPath.row];
                
            }
            else
            {
                vCell.infoDescriptionLB.text=[_titleArray objectAtIndex:3+ indexPath.row];
                vCell.infoEditTextField.placeholder=[_infoDetailArray objectAtIndex:3+indexPath.row];
            }
            
            
        }
         vCell.infoEditTextField.delegate=self;
        vCell.infoEditTextField.tag=indexPath.row+1000*indexPath.section;
        return vCell;
    }
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if([DSFUserInfo shareUserInstance].isTeachLogin)
        return 2;
    else
        return 3;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        //个人详情页面
        UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从手机相册选择",nil];
        [sheet showInView:self.view];
    }
    else
    {
        
    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if(2==buttonIndex)
        return;
    
    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate =self;
    picker.allowsEditing=YES;
    
    if(1  == buttonIndex) {
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    else if(0 == buttonIndex) {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    
    [self presentViewController:picker animated:YES completion:nil];
    //	[self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo {
    
    [picker dismissModalViewControllerAnimated:YES];
    
    NSString *registrationId = [APService registrationID];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableArray *imageArr = [[NSMutableArray alloc] initWithObjects:image, nil];
    
    [CoModel uploadImage:^(BOOL result, NSMutableArray *imageUrlList, NSError *error) {
        if (result) {
            [DSFUserInfo shareUserInstance].icon=imageUrlList[0];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            [CoModel modify:^(BOOL result, NSString *failMessage, NSError *error) {
                if (result)
                {
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
                    [_contentTableView reloadData];
                }else
                    [MBProgressHUD hideHUDForView:self.view animated:YES];
            } withIcon:imageUrlList[0] andPushId:registrationId];
        }else
            [MBProgressHUD hideHUDForView:self.view animated:YES];
    } withImage:imageArr andType:1];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
